<?php

namespace App\Models;

class BarsGoodsModel
{
    /**
     * 查询当前商品
     * entry gid
     * return int
     * */
    public static function getGoodsSingle($conn,$gid){
        $sql ="SELECT `bid`,`cid`,`name`,`synopsis`,`price`,`integral`,`original_price`,`cost_price`,`sales`,`reality_sales`,`img`,`video`,`details`,`stock`,`status`,`type`,`discount`,`rebate`,`views`,`sort`,`sku` FROM `bars_goods` WHERE `id` = $gid";
        return $conn->readConn($sql);
    }
    /**
     * 查询当前商品sku
     * entry gid,skuid
     * return int
     * */
    public static function getGoodsSku($conn,$gid,$skuid){
        $sql ="SELECT `sku` FROM `bars_goods` WHERE `id` = $gid";
        $sku = json_decode($conn->readConn($sql)["sku"], true);
        return $sku[0]["arr"][$skuid];
    }
    /**
     * 查询当前商品奖励积分
     * entry gid
     * return int
     * */
    public static function getGoodsTimes($conn,$gid){
        $sql ="SELECT `times` FROM `bars_goods` WHERE `id` = $gid";
        return $conn->readConn($sql)["times"];
    }
    /**
     * 添加商品
     * entry BarsGoodsExample
     * return int
     * */
    public static function insertBarsGoods($conn,$BarsGoodsExample){
        $BarsGoodsExample->integral = $BarsGoodsExample->integral ? $BarsGoodsExample->integral:0;
        $fundsSql ="INSERT INTO `bars_goods`(`cid`,`name`,`synopsis`,`price`,`integral`,`sales`,`img`,`details`,`stock`,`status`,`type`,`views`,`sort`,`sku`,`times`) VALUES ($BarsGoodsExample->cid,'$BarsGoodsExample->name','$BarsGoodsExample->synopsis',$BarsGoodsExample->price,$BarsGoodsExample->integral,'$BarsGoodsExample->sales','$BarsGoodsExample->img','$BarsGoodsExample->details','$BarsGoodsExample->stock','$BarsGoodsExample->status','$BarsGoodsExample->type','$BarsGoodsExample->views','$BarsGoodsExample->sort','$BarsGoodsExample->sku',0)";
        return $conn->executeConn($fundsSql);
    }
    /**
     * 修改商品可添加字段
     * entry BarsGoodsExample
     * return int
     * */
    public static function upBarsGoodsInside($conn,$BarsGoodsExample)
    {
        $joinSql = "";
        if ($BarsGoodsExample->bid != null) {
            $joinSql .= " `bid` = '$BarsGoodsExample->bid',";
        }
        if ($BarsGoodsExample->cid != null) {
            $joinSql .= " `cid` = '$BarsGoodsExample->cid',";
        }
        if ($BarsGoodsExample->name != null) {
            $joinSql .= " `name` = '$BarsGoodsExample->name',";
        }
        if ($BarsGoodsExample->synopsis != null) {
            $joinSql .= " `synopsis` = '$BarsGoodsExample->synopsis',";
        }
        if ($BarsGoodsExample->price != null) {
            $joinSql .= " `price` = $BarsGoodsExample->price,";
        }
        if ($BarsGoodsExample->integral != null) {
            $joinSql .= " `integral` = $BarsGoodsExample->integral,";
        }
        if ($BarsGoodsExample->original_price != null) {
            $joinSql .= " `original_price` = $BarsGoodsExample->original_price,";
        }
        if ($BarsGoodsExample->cost_price != null) {
            $joinSql .= " `cost_price` = $BarsGoodsExample->cost_price,";
        }
        if ($BarsGoodsExample->sales != null) {
            $joinSql .= " `sales` = '$BarsGoodsExample->sales',";
        }
        if ($BarsGoodsExample->reality_sales != null) {
            $joinSql .= " `reality_sales` = '$BarsGoodsExample->reality_sales',";
        }
        if ($BarsGoodsExample->img != null) {
            $joinSql .= " `img` = '$BarsGoodsExample->img',";
        }
        if ($BarsGoodsExample->video != null) {
            $joinSql .= " `video` = '$BarsGoodsExample->video',";
        }
        if ($BarsGoodsExample->details != null) {
            $joinSql .= " `details` = '$BarsGoodsExample->details',";
        }
        if ($BarsGoodsExample->stock != null) {
            $joinSql .= " `stock` = $BarsGoodsExample->stock,";
        }
        if ($BarsGoodsExample->status != null) {
            $joinSql .= " `status` = $BarsGoodsExample->status,";
        }
        if ($BarsGoodsExample->type != null) {
            $joinSql .= " `type` = $BarsGoodsExample->type,";
        }
        if ($BarsGoodsExample->discount != null) {
            $joinSql .= " `discount` = '$BarsGoodsExample->discount',";
        }
        if ($BarsGoodsExample->rebate != null) {
            $joinSql .= " `rebate` = '$BarsGoodsExample->rebate',";
        }
        if ($BarsGoodsExample->views != null) {
            $joinSql .= " `views` = '$BarsGoodsExample->views',";
        }
        if ($BarsGoodsExample->sort != null) {
            $joinSql .= " `sort` = '$BarsGoodsExample->sort',";
        }
        if ($BarsGoodsExample->sku != null) {
            $joinSql .= " `sku` = '$BarsGoodsExample->sku',";
        }
        if ($BarsGoodsExample->times != null) {
            $joinSql .= " `times` = '$BarsGoodsExample->times',";
        }
        if ($joinSql != null) {
            $innerJoin = rtrim($joinSql, ",");
            $upUserSql = "UPDATE  `bars_goods` SET $innerJoin WHERE `id` = '$BarsGoodsExample->id'";
            return $conn->executeConn($upUserSql);
        }
    }
}